<template>
  <BaseForm v-model="formDataModel" @close="$emit('callBack')" />

</template>

<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import ConsoleRolePermissionApi from '/@/api/console/ConsoleRolePermissionApi.ts';
import { translate } from "/@/i18n";
import FORM_FIELD_TYPE from "/@/constants/FormFieldType.ts";
import ConsolePermissionApi from "/@/api/console/ConsolePermissionApi.ts";
import treeUtil from "/@/utils/treeUtil.ts";
import ConsoleRoleApi from "/@/api/console/ConsoleRoleApi.ts";
import ConsoleUserApi from "/@/api/console/ConsoleUserApi.ts";
defineOptions({
  name: 'ConsoleRolePermissionEdit',
})
const props = defineProps({
  modelValue: {

  }
})
const emit = defineEmits(['fetch-data', 'callBack'])

const formRef = ref<FormInstance>();
const title = ref<string>('');
const dialogFormVisible = ref<boolean>(false);

const defaultQuery = ref(props.modelValue.query)

const formDataModel = ref({
  columnCount: 1,
  expandLimit: 6,
  autoFit: false,
  columnWidth: '300px',
  labelPosition: 'left',
  labelWidth: '100px',
  modelData: {},  //表单参数值 初始化
  callback: async (any: object) => {  //表单执行成功后回调
    //完成数据提交
    const { message } = await ConsoleUserApi.assignRole({
      companyAccountId: any.id,
      roleIds: any.roleIds.join(',')
    })
    $baseMessage(message, 'success', 'vab-hey-message-success');
    dialogFormVisible.value = false


    //事件及数据推送到上一层父类
    emit('callBack', any); //把表单数据推送到上一层
  },
  items: [  //表单item数据初始化
    {
      label: "角色",
      field: "roleIds",
      clearable: true,
      type: FORM_FIELD_TYPE.SELECT,
      placeholder: "请输入",

      config: {
        placeholder: '选择角色',
        clearable: true,
        optionsFunction: async () => {  //获取网络数据源 初始化组件
          const { data } = await ConsoleRoleApi.getPageData({
            _size: 10000
          })
          let menus = data.records
          return treeUtil.treeData(menus, [], { id: 'id', parentId: 'pid', children: 'children' });
        },
        props: {
          label: 'roleName',
          value: 'id',
          emitPath: false,
          multiple: true
        }
      },
      rule: {
        trigger: 'blur',
        validator: (rule: any, value: any, callback: any) => {  //绑定验证
          callback();
        },
      },

    }
  ],
})






//页面数据加载
onMounted(async () => {

  //初始化数据model
  if (props.modelValue.query.consoleUserId) {


    //todo 更新角色
  }
  console.log(props.modelValue);
  const { data } = await ConsoleUserApi.getDetail({
    companyAccountId: props.modelValue.query.consoleUserId   //从路由中获取参数
  })

  data.roleIds = data.companyRoleList && data.companyRoleList.map(value => value.id) || []

  formDataModel.value.modelData = data;

})


//方法暴露区域  todo
defineExpose({

})

</script>
